package main

import (
	"fmt"
	"log"

	"github.com/eatmoreapple/openwechat"
	"github.com/hollson/wxbot/handlers"
	"github.com/hollson/wxbot/utils"
)

// ConsoleQrCode 控制台二维码登录
func ConsoleQrCode(uuid string) {
	fmt.Printf(" \U0001FAE7  \U0001FAE7  \U0001FAE7 欢迎使用微信机器人(v%s)，请扫码登录 \U0001FAE7  \U0001FAE7  \U0001FAE7\n", utils.Version())
	qrcode := utils.New(utils.ConsoleColors.NormalWhite, utils.ConsoleColors.BrightGreen, utils.QRCodeRecoveryLevels.Low)
	fmt.Println(qrcode.QRCodeString(fmt.Sprintf("https://login.weixin.qq.com/l/%s", uuid)))
}

func main() {
	bot := openwechat.DefaultBot(openwechat.Desktop)
	bot.MessageHandler = handlers.Handler
	bot.UUIDCallback = ConsoleQrCode

	// 热登录
	err := bot.HotLogin(openwechat.NewFileHotReloadStorage("storage.json"))
	if err != nil {
		if err = bot.Login(); err != nil {
			log.Printf("login error: %v \n", err)
			return
		}
	}

	// self, _ := bot.GetCurrentUser()
	// fmt.Println(self.ID())
	// fmt.Println("===> ", self.Self().NickName)

	// 阻塞主goroutine, 直到发生异常或者用户主动退出
	go bot.Block()
	select {}
}
